package com.chat.datea.ui;

import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chat.datea.R;
import com.chat.datea.adapter.MainActivity_ViewPager_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.fragment.MoreFragment;
import com.chat.datea.fragment.SearchFragment;
import com.chat.datea.utils.fragment.ReplaceFragment;


/**
 * 王锦予
 **/
public class MainFragment extends BaseFragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener , View.OnClickListener{
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private RadioButton radioButtonMy, radioButtonMusic, radioButtonFind;
    private ImageButton imageButtonSearch, imageButtonMore;
    private MainActivity_ViewPager_Adapter pagerAdapter;
    private SearchFragment searchFragment;
    private MoreFragment moreFragment;
    private FragmentManager fManager;

    public MainFragment(FragmentManager fManager) {
        this.fManager = fManager;
    }


    @Override
    protected void initData() {

        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(1);
        initViewListener();
    }

    @Override
    protected void initView() {

        viewPager = (ViewPager) view.findViewById(R.id.main_viewpager);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        radioButtonMy = (RadioButton) view.findViewById(R.id.radioButtonMy);
        radioButtonMusic = (RadioButton) view.findViewById(R.id.radioButtonMusic);
        radioButtonFind = (RadioButton) view.findViewById(R.id.radioButtonFind);
        imageButtonSearch = (ImageButton) view.findViewById(R.id.imageButtonSearch);
        imageButtonMore = (ImageButton) view.findViewById(R.id.imageButtonMore);
        pagerAdapter = new MainActivity_ViewPager_Adapter(fManager,getChildFragmentManager());
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view =inflater.inflate(R.layout.fragment_main, null);
        return view;
    }




    /**
     * 初始化视图监听
     */
    private void initViewListener() {
        viewPager.addOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);

        imageButtonSearch.setOnClickListener(this);
        imageButtonMore.setOnClickListener(this);
    }

    /**
     * viewpager的三个监听
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        // viewpager第几个页被选择
        switch (position) {
            case 0:
                radioButtonMy.setChecked(true);
                break;
            case 1:
                radioButtonMusic.setChecked(true);
                break;
            case 2:
                radioButtonFind.setChecked(true);
                break;
        }
    }

    private void setTextColor(int myColor, int musicColor, int findColor) {
        radioButtonMy.setTextColor(myColor);
        radioButtonMusic.setTextColor(musicColor);
        radioButtonFind.setTextColor(findColor);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * radioGroup的选择变化监听
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioButtonMy:
                viewPager.setCurrentItem(0);
                setTextColor(Color.WHITE, Color.BLACK, Color.BLACK);
                break;
            case R.id.radioButtonMusic:
                viewPager.setCurrentItem(1);
                setTextColor(Color.BLACK, Color.WHITE, Color.BLACK);
                break;
            case R.id.radioButtonFind:
                viewPager.setCurrentItem(2);
                setTextColor(Color.BLACK, Color.BLACK, Color.WHITE);
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageButtonSearch:

                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                searchFragment = new SearchFragment();
                new ReplaceFragment(searchFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);
                transaction.commit();
                break;
            case R.id.imageButtonMore:

                FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
                moreFragment = new MoreFragment();
                new ReplaceFragment(moreFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);
                transaction1.commit();
                break;
        }
    }


}
